package gateway.server.net;

import gateway.api.annotation.GatewayType;
import gateway.api.annotation.Named;
import gateway.server.http.HttpServerHandler;
import gateway.server.util.Const;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;

/**
 * @author Jin Zheng
 * @since 1.0 2020-09-10
 */
@Named(value = "httpConnector", type = GatewayType.HTTP)
public class HttpConnector extends ServerConnector
{

	public HttpConnector()
	{
	}

	@Override
	protected ChannelInitializer<SocketChannel> channelInitializer()
	{
		return new ChannelInitializer<>()
		{
			@Override
			protected void initChannel(SocketChannel sc) throws Exception
			{
				ChannelPipeline cp = sc.pipeline();

				/*SslContext sslContext = sslContext();
				if (sslContext != null)
				{
					cp.addLast(sslContext.newHandler(sc.alloc()));
				}*/

				cp.addLast(Const.HTTP_CODEC, new HttpServerCodec());
				cp.addLast(Const.HTTP_OBJECT, new HttpObjectAggregator(Short.MAX_VALUE * 10));
				cp.addLast(Const.HTTP_HANDLER, new HttpServerHandler(id, engine));
			}
		};
	}

	@Override
	protected GatewayType type()
	{
		return GatewayType.HTTP;
	}


}
